//
//  SXOAuthViewController.swift
//  sina
//
//  Created by shengxinwei on 16/5/22.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SVProgressHUD


private let userInfoKey = "com.jianshu.userInfoKey"

class SXOAuthViewController: UIViewController , UIWebViewDelegate {

    let webView = UIWebView()

    
    override func loadView() {
        //切换根视图界面  为 web界面
        view = webView
        //解决web页面加载无网络情况下,上拉出现黑条
        webView.opaque = false
        webView.backgroundColor = UIColor.redColor()
        //设置webView代理
        webView.delegate = self
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.lightGrayColor()
        setBarItem()
        loadOauthView()
      
    }
    
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        print()
        SVProgressHUD.dismiss()
    }

    
    //打开授权界面
    func loadOauthView() {
        
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL.init(string: urlString)
        let request = NSURLRequest.init(URL: url!)
        print(request)
        //web 加载请求
        webView.loadRequest(request)
    }
    
    //webView开始加载
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    //webView加载结束
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
   
    
    
    
    
    //设置返回按钮
    func setBarItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: #selector(dismissBack))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测试账户", style: .Plain, target: self, action: #selector(SXOAuthViewController.showTestAccount))
    }
    
    //关闭操作
    @objc private func dismissBack() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //点击获取测试账户
    @objc private  func showTestAccount() {
        let jsString = "document.getElementById('userId').value='407425580@qq.com';document.getElementById('passwd').value='XIN2326228wei'"
        //将字符串转换成js执行,.......忘了
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    //webView页面加载回调 
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let url = request.URL?.absoluteString ?? ""
        let code = "code="
        if url.containsString(code) {
            let range = url.rangeOfString(code)
            let codeNo = url.substringFromIndex((range?.endIndex)!)
            //print(codeNo)
            //获取code码后,根据code码,获取token
            SXUserAccountViewModel.sharedAccount.loadToken(codeNo, finished: { (isSuccess) in
                //将现有视图移除做跳转
                self.dismissViewControllerAnimated(false, completion: {
                    //发送通知切换根控制器
                    NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootViewController", object: nil)
                    
                })
            })
            //不让它跳转页面
            return false
        }
        
        return true
    }
    
}
